home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
007
/
cstdio.arc
/
SRC.ARC
/
FGETS.C
< prev
next >
Wrap
C/C++ Source or Header
|
1984-07-31
|
548b
|
31 lines
/* fgets.c - get string from stream.
K & R page 155.
Entered - G. R. Mansfield. 84/06/08.
Ver 1.0-4731.
*/
#include <stdio.h>
char *fgets(s, n, fp) /* get at most n characters from stream */
char *s;
int n;
FILE *fp;
{
int c;
char *cs;
cs = s;
while (--n > 0 && (c = getc(fp)) != EOF) {
if ((*cs++ = c) == '\n')
break;
}
if (cs - s >= 2) { /* delete possible CR from end of line */
if (*(cs-2) == '\r') {
cs--;
*(cs-1) = '\n';
}
}
*cs = '\0';
return((c == EOF && cs == s) ? NULL : s);
}